package com.jinglin.web.task;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinglin.component.EsSearchComponent;
import com.jinglin.component.RedisComponent;
import com.jinglin.entity.constants.Constants;
import com.jinglin.entity.dto.VideoPlayInfoDto;
import com.jinglin.entity.enums.SearchOrderTypeEnum;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.po.VideoInfoFilePost;
import com.jinglin.entity.po.VideoPlayHistory;
import com.jinglin.redis.RedisUtils;

import com.jinglin.service.VideoInfoFilePostService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.service.VideoPlayHistoryService;
import com.jinglin.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.similarities.Lambda;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
@Component
public class ExecuteQueueTask {
    private ExecutorService executorService = Executors.newFixedThreadPool(Constants.LENGTH_10);
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private VideoInfoFilePostService videoInfoFilePostService;
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private RedisComponent redisComponent;
    @Autowired
    private VideoPlayHistoryService videoPlayHistoryService;
/*    @Autowired
    private EsSearchComponent esSearchComponent;*/
/*    @PostConstruct
    public void consumeTransferFileQueue(){
        executorService.execute(()->{
            while (true){
                try {
                    // 从redis中拿到待转码的文件信息。
                    VideoInfoFilePost videoInfoFile = (VideoInfoFilePost) redisUtils.rpop(Constants.REDIS_KEY_FILE_TRANSFER_QUEUE);
                    // 如果没有待转码的文件，线程休息1.5s
                    if (videoInfoFile == null){
                        Thread.sleep(1500);
                        continue;
                    }
                    videoInfoFilePostService.transferVideoFile(videoInfoFile);
                } catch (Exception e) {
                    log.error("获取转码文件队列信息失败",e);
                }
            }
        });
    }*/
/*
    @PostConstruct
    public void consumeVideoPlayQueue(){
        executorService.execute(()->{
            while (true){
                try {
                    // 从redis中拿到播放队列
                    VideoPlayInfoDto videoPlayInfoDto = (VideoPlayInfoDto) redisUtils.rpop(Constants.REDIS_KEY_QUEUE_VIDEO_PLAY);
                    // 如果没有播放队列，线程休息1.5s
                    if (videoPlayInfoDto == null){
                        Thread.sleep(1500);
                        continue;
                    }
                    // 更新视频浏览量
                    videoInfoService.addReadCount(videoPlayInfoDto.getVideoId());
                        // 更新视频上传播放时间
                    LambdaUpdateWrapper<VideoInfo> wrapper = new LambdaUpdateWrapper<>();
                    wrapper.eq(VideoInfo::getVideoId,videoPlayInfoDto.getVideoId());
                    wrapper.set(VideoInfo::getLastPlayTime,LocalDateTime.now());
                    videoInfoService.update(wrapper);
                    // 如果处于登录状态的播放，则记录历史
                    if (!StringTools.isEmpty(videoPlayInfoDto.getUserId())){
                        VideoPlayHistory videoPlayHistory = new VideoPlayHistory();
                        BeanUtils.copyProperties(videoPlayInfoDto,videoPlayHistory);
                        videoPlayHistory.setLastUpdateTime(LocalDateTime.now());
                        videoPlayHistoryService.saveVideoHistory(videoPlayHistory);
                    }

                    // 记录当天24小时浏览量。
                    redisComponent.recordVideoPlayCount(videoPlayInfoDto.getVideoId());


                    // 更新es播放数量()
                    // TODO 不是很懂ES的语法，这里有一个bug，对于我来说有点难度，因此不在ES中拿这个数据videoPlayCount。

*//*
                    esSearchComponent.updateDocCount(videoPlayInfoDto.getVideoId(), SearchOrderTypeEnum.VIDEO_PLAY.getField(), 1);
*//*

                } catch (Exception e) {
                    log.error("获取转码文件队列信息失败",e);
                }
            }
        });
    }*/
}
